package com.beyondsoft.mcms.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.beyondsoft.mcms.common.Constants;
import com.beyondsoft.mcms.domain.Category;
import com.beyondsoft.mcms.domain.GroupInfo;
import com.beyondsoft.mcms.domain.Item;
import com.beyondsoft.mcms.service.MemberInfoService;

@RequestMapping("/groupinfoes")
@Controller
@RooWebScaffold(path = "groupinfoes", formBackingObject = GroupInfo.class)
public class GroupInfoController {

    @Autowired
    MemberInfoService memberInfoService;

    void populateEditForm(Model uiModel, GroupInfo groupInfo) {
        uiModel.addAttribute("groupInfo", groupInfo);
        uiModel.addAttribute("businesstypes", Item.findItemsByCategoryAndIsValid(Category.findCategory(new Long(Constants.BUSINESS_TYPE)), true).getResultList());
        uiModel.addAttribute("positiontypes", Item.findItemsByCategoryAndIsValid(Category.findCategory(new Long(Constants.POSITION_TYPE)), true).getResultList());
    }

    @RequestMapping(value = "/{id}", produces = "text/html")
    public String show(@PathVariable("id") GroupInfo groupInfo, Model uiModel) {
        uiModel.addAttribute("groupinfo", groupInfo);
        uiModel.addAttribute("itemId", groupInfo.getId());
        uiModel.addAttribute("memberinfoes", memberInfoService.findMemberInfoByGroupInfo(groupInfo));
        return "groupinfoes/show";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        GroupInfo groupInfo = GroupInfo.findGroupInfo(id);
        groupInfo.setBusinessTypes(null);
        groupInfo.setPositionTypes(null);
        groupInfo.remove();
        uiModel.asMap().clear();
        uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
        uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
        return "redirect:/groupinfoes";
    }
}
